package handler

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"user-sys/models"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	image := c.PostForm("image")
	likes, _ := strconv.Atoi(c.PostForm("likes"))

	if title == "" || content == "" || image == "" || likes == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Video{
		Title:   title,
		Content: content,
		Image:   image,
		Likes:   likes,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}
func List(c *gin.Context) {
	var video []models.Video
	models.DB.Debug().Find(&video)
	c.JSON(200, gin.H{
		"code": 200,
		"data": video,
		"msg":  "查询成功",
	})
	return

}
